#include "mainwidget.h"
#include <QDebug>
#include <QLineEdit>
#include <QMessageBox>
#include <QObject>
#include <QPainter>
#include <QPicture>
#include <QPushButton>
#include <QSharedPointer>
#include <QSqlError>
#include <QSqlQuery>
#include <QTextEdit>
#include <QWidget>
#include <Qpixmap>
#include <QCheckBox>
#include <QSettings>
#include <QLabel>
#include "iphonewindow.h"
#include "mypushbutton.h"


MainWidget::MainWidget(QWidget *parent)
    : IphoneWindow(parent)
{

    QSettings setting("config.ini", QSettings::IniFormat);
    QString account = setting.value("section/account").toString();
    QString password = setting.value("section/password").toString();

    QLineEdit *accountEdit = new QLineEdit(this); //账号
    accountEdit->setText(account);
    accountEdit->resize(231, 40);
    accountEdit->move(79, 307);
    accountEdit->setPlaceholderText("请输入账号");

    QLineEdit *passwordEdit = new QLineEdit(this); //密码
    passwordEdit->setText(password);
    passwordEdit->resize(231, 40);
    passwordEdit->move(79, 371);
    passwordEdit->setPlaceholderText("请输入密码");

    QCheckBox *checkBox = new QCheckBox(this);
    checkBox->setText("记住密码");
    checkBox->move(240, 430);
    checkBox->setCheckState(Qt::Checked);

    //用户登录，查询数据
    QPushButton *loginBtn = new QPushButton(this);
    loginBtn->resize(226, 41);
    loginBtn->setText("登录");
    loginBtn->move(82, 465);
    loginBtn->setStyleSheet(
        "QPushButton{background-color: rgb(255,201,89);border:2px groove "
        "gray;border-radius:10px;padding:2px 4px;border-style: outset;}"
        "QPushButton:hover{background-color:rgb(242,206,132); color: black;}"
        "QPushButton:pressed{background-color:rgb(255,201,89);border-style: inset;}");

    connect(loginBtn, &QPushButton::clicked, this, [=]() {
        //this->StartHomePage();
        //接收用户输入
        QString account = accountEdit->text();   //账号
        QString password = passwordEdit->text(); //密码
        //插入数据
        QSqlQuery query; //操作数据库
        QString qs = QString("select * from user where account = '%1' and password = '%2' ")
                         .arg(account)
                         .arg(password);

        if (!query.exec(qs)) {
            //查询失败,输出错误信息
            qDebug() << query.lastError().text();
            return;
        }

        //获取查询到的信息
        if (query.next()) {
            QMessageBox::information(this, "登录", "登录成功");
            this->hide();
            IphoneWindow *mainMenu = new IphoneWindow();
            mainMenu->StartHomePage();
            mainMenu->show();
            if (checkBox->isChecked()) {
                QSettings setting("config.ini", QSettings::IniFormat); //配置文件在工程目录下
                //把账号密码写入配置文件中
                setting.beginGroup("section"); //节开始
                setting.setValue("account", account);
                setting.setValue("password", password);
                setting.endGroup(); //结束
            }
            else{
                QSettings setting("config.ini", QSettings::IniFormat); //配置文件在工程目录下
                //把账号密码写入配置文件中
                setting.beginGroup("section"); //节开始
                setting.setValue("account", "");
                setting.setValue("password", "");
                setting.endGroup(); //结束
            }
        } else {
            QMessageBox::information(this, "登录", "登录失败");
        }
    });

    //用户注册，向数据库中插入数据
    QPushButton *registerBtn = new QPushButton(this);
    registerBtn->setText("注册");
    registerBtn->move(250, 510);
    registerBtn->setFlat(true);

    connect(registerBtn, &QPushButton::clicked, this, [=]() {
        //接收用户输入
        QString account = accountEdit->text();   //账号
        QString password = passwordEdit->text(); //密码
        //插入数据
        QSqlQuery query; //操作数据库
        QString qs = QString("insert into user(account,password) values('%1','%2')")
                         .arg(account)
                         .arg(password);
        if (query.exec(qs)) {
            //插入成功
            QMessageBox::information(this, "注册", "注册成功");

        } else {
            QMessageBox::information(this, "注册", "注册失败");
            qDebug() << query.lastError().text();
            return;
        }
    });



    //底部图标
    MyPushButton *loginIcon1 = new MyPushButton(":/res/UI/icon_QQ.png");
    MyPushButton *loginIcon2 = new MyPushButton(":/res/UI/weixin.png");
    MyPushButton *loginIcon3 = new MyPushButton(":/res/UI/icon_phone.png");
    loginIcon1->setParent(this);
    loginIcon2->setParent(this);
    loginIcon3->setParent(this);

    loginIcon1->resize(50, 50);
    loginIcon1->move(10, 584);
    loginIcon2->move(100, 584);
    loginIcon3->move(190, 584);

    this->setWindowTitle("高校社区消费交流体验平台");

    // logo
    QLabel *logoImg = new QLabel(this);
    logoImg->setFixedSize(250, 150);
    logoImg->setStyleSheet("border: none; color: black; font-size: 10px; image: url(:/image.png); image-position: top center;");
    // 使用HTML语法来定义图片和文字
    logoImg->setText("<img src=':/res/UI/logo.png' width='250' height='150'><div style=></div>"); //'text-align: center;'
    //HLayout->addWidget(ShopImg);
    logoImg->move(70, 100);

    // account
    QLabel *accountIcon = new QLabel(this);
    accountIcon->setFixedSize(20, 20);
    accountIcon->setStyleSheet("border: none; color: black; font-size: 10px; image: url(:/image.png); image-position: top center;");
    // 使用HTML语法来定义图片和文字
    accountIcon->setText("<img src=':/res/UI/account.png' width='20' height='20'><div style=></div>"); //'text-align: center;'
    //HLayout->addWidget(ShopImg);
    accountIcon->move(55, 317);

    // password
    QLabel *passwordIcon = new QLabel(this);
    passwordIcon->setFixedSize(20, 20);
    passwordIcon->setStyleSheet("border: none; color: black; font-size: 10px; image: url(:/image.png); image-position: top center;");
    // 使用HTML语法来定义图片和文字
    passwordIcon->setText("<img src=':/res/UI/password.png' width='20' height='20'><div style=></div>"); //'text-align: center;'
    //HLayout->addWidget(ShopImg);
    passwordIcon->move(55, 380);

}
